import { _decorator, Node, find, instantiate, Prefab, resources, Sprite, tween, Animation, Component } from 'cc';
import { ZTool } from '../ZTool';
import Banner from '../Banner';
import { UIManager } from '../UIManager';
;

const { ccclass, property } = _decorator;

@ccclass('TreasureBoxPanel')
export default class TreasureBoxPanel extends Component {
    @property(Sprite)
    jdt: Sprite | null = null;//进度条

    Panel: Node = null;

    index: number = 0;

    gived: boolean = false;
    sussCb: Function = null;

    public static Init() {
        resources.load("Prefab/TreasureBoxPanel", Prefab, (err, prefab) => {
            let node = instantiate(prefab);
            node.setParent(UIManager.Instance.node.getChildByName("Canvas"));
            node.setPosition(0, 0, 0);
            node.getComponent(TreasureBoxPanel).Show();
        });

    }

    Show(sussCb: Function = null) {
        this.IsShow2 = false;
        this.IsShow = false;
        this.sussCb = sussCb;
        this.gived = false;
        this.index = 0;
        this.unschedule(this.Sub_index);
        this.schedule(this.Sub_index, 0.1);
    }

    //定时扣
    Sub_index() {
        if (this.index > 0) this.index -= 1;
    }
    IsShow: boolean = false;
    IsShow2: boolean = false;

    //点击宝箱按钮
    OnButtonClick() {
        this.index += ZTool.GetRandom(5, 10);
        if (this.index >= 60 && !this.IsShow) {
            Banner.Instance.ShowVideoAd(() => { this.GiveReward(); });
            this.IsShow = true;
            this.scheduleOnce(() => {
                this.sussCb && this.sussCb();
                this.node.destroy();
            }, 1);
        }
    }
    protected update(dt: number): void {
        this.jdt.fillRange = this.index / 100;
    }

    protected lateUpdate(dt: number): void {
        if (this.index > 0) this.index -= 0.1;
    }

    //提供奖励
    GiveReward() {
        if (this.gived) return;
        this.gived = true;
        this.sussCb && this.sussCb();
    }
}